package org.i_plus.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i_plus.Assistance;
import org.i_plus.listener.SessionListener;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private MemberService service ;
	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService)context.getBean("MemberService");
	}
	
	
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//接收資料	
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		String securenumber = req.getParameter("securenumber");
		req.setAttribute("account", account);
		req.setAttribute("password", password);
		res.setContentType("text/html; charset=UTF-8");
		//驗證資料
		Map<String,String> errors = new HashMap<String,String>();
		req.setAttribute("errors", errors);
		if(account == null ||  account.trim().length() ==0 ){
//			errors.put("account", "請輸入使用者");
			res.getWriter().print("請輸入使用者");
			return;
		}
		if(password == null ||password.trim().length() ==0){
//			errors.put("password", "請輸入密碼");
			res.getWriter().print("請輸入密碼");
			return;
		}
		if(securenumber == null ||password.trim().length() ==0){
//			errors.put("securenumber", "請輸入驗證碼");
			res.getWriter().print("請輸入驗證碼");
			return;
		}
//		if(errors != null && !errors.isEmpty()){
//			req.getRequestDispatcher("login.jsp").forward(req, res);
//			return;
//		}
		//核對帳號密碼
		
				HttpSession session = req.getSession();
				String securenumberTrue = (String)session.getAttribute("SecureNumber");
				if(!securenumber.equalsIgnoreCase(securenumberTrue)){
					//errors.put("securenumber", "驗證碼錯誤");
					res.getWriter().print("驗證碼錯誤");
					return;
				}
				MemberBean bean = service.login(account, password);
				if(bean == null){
//				　errors.put("login", "使用者錯誤或密碼錯誤");
					res.getWriter().print("使用者名稱或密碼錯誤");
					return;
				}
//				if(errors != null && !errors.isEmpty()){
//					req.getRequestDispatcher("login.jsp").forward(req, res);	
//					return;
//				}
				
				session.setAttribute("user", bean);//使用者的資料
				session.setAttribute("userpic", Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));//使用者的圖片
				SessionListener.sessionids.put(account,req.getSession().getId());//帳號和sessionid綁定 為了聊天功能
				
//				Cookie cookie1 = new Cookie("user", bean.getAccount());
//				cookie1.setMaxAge(Integer.MAX_VALUE);
//				Cookie cookie2 = new Cookie("userpic", Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));
//				cookie2.setMaxAge(Integer.MAX_VALUE);
//				res.addCookie(cookie1);
//				res.addCookie(cookie2);
				
//				String uri = (String)session.getAttribute("destination");
//				if(uri !=null ||uri.trim().length() !=0){
//					session.removeAttribute("destination");//destination在filter
//					res.sendRedirect(uri);
//				}else{
					res.getWriter().print("ok");
					return;
//				}
	}
	
	
	
	

}
